package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiDZColor;
import com.hilaver.dzmis.codetable.CtDesignType;
import com.hilaver.dzmis.codetable.CtSeasonNumber;
import com.hilaver.dzmis.codetable.CtSendBy;
import com.hilaver.dzmis.codetable.CtYarnMaterial;
import com.hilaver.dzmis.codetable.CtYarnMeasure;
import com.hilaver.dzmis.dao.OrderColorTestDAO;
import com.hilaver.dzmis.order.OrderColorTest;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class OrderColorTestServiceImpl extends AbstractBaseService {
	
	private CtServiceImpl ctService;
	
	private OrderColorTestDAO dao;
	
	public OrderColorTestServiceImpl(){
		this.dao = new OrderColorTestDAO();
		this.ctService = new CtServiceImpl();
	}
	
	public String edit(OrderColorTest obj) throws Exception {
		String operation = null;
		if(obj.getId() != null){
			OrderColorTest old = (OrderColorTest)this.dao.get(OrderColorTest.class.getName(), obj.getId());
			if(obj.getPhotoName() == null){
				obj.setPhoto(old.getPhoto());
				obj.setPhotoName(old.getPhotoName());
			}
			if(obj.getPhoto1Name() == null){
				obj.setPhoto1(old.getPhoto1());
				obj.setPhoto1Name(old.getPhoto1Name());
			}
			this.dao.getSession().evict(old);
			operation = "02";
		}else{
			operation = "01";
		}
		this.dao.saveOrUpdate(obj);
		super.editSysMbox(obj, operation);
		return "{success: true}";
	}

	@Override
	public String delete(int id) throws Exception {
		super.delete(OrderColorTest.class.getName(), id);
		return super.delete(OrderColorTest.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		OrderColorTest oct = (OrderColorTest)this.dao.get(OrderColorTest.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(oct));
		
		xmlSB.append(this.toXMLFragment(oct));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getWithDescription(int id) throws Exception {
		OrderColorTest oct = (OrderColorTest)this.dao.get(OrderColorTest.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(this.toXMLFragment(oct));

		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAllPagination(int page, String sort,
			String order, String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List objList = this.dao.getAllPagination(OrderColorTest.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(this.toXMLFragment((OrderColorTest)obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(OrderColorTest.class.getName(),
				filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	
	public String getAll(Integer piId) throws Exception{
		
		return null;
	}

	public String getMaxReferenceIndex() throws Exception{
		List<Integer> referenceIndexList = this.dao.getMaxReferenceIndex();
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(Constants.XML_CDATA[0]);
		if(referenceIndexList.size() == 0 || referenceIndexList.get(0) == null){
			xmlSB.append("000001");
		}else{
			Integer index = referenceIndexList.get(0);
			xmlSB.append(StringUtils.to6Digital(++index));
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String toXMLFragment(OrderColorTest oct) throws Exception {
		StringBuffer xmlSB = new StringBuffer();
		
		xmlSB.append("<ctSeasonNumber>");
		xmlSB.append(this.ctService.getDescription(CtSeasonNumber.class.getName(), oct.getCtSeasonNumber(), locale));
		xmlSB.append("</ctSeasonNumber>");
		
		
		BiDZColor biDZColor = oct.getBiDZColor();
		
		writeFile(realFileDir + oct.getPhotoName(), oct.getPhoto());
		writeFile(realFileDir + oct.getPhoto1Name(), oct.getPhoto1());
		//writeFile(realFileDir + biDZColor.getPhoto1Name(), biDZColor.getPhoto1());
		//writeFile(realFileDir + biDZColor.getPhoto2Name(), biDZColor.getPhoto2());
		
		xmlSB.append("<dzColorId>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biDZColor.getId());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</dzColorId>");
		
		xmlSB.append("<dzColorCode>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biDZColor.getDzColorCode());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</dzColorCode>");
		
		if(biDZColor.getBiFranceCustomer() != null){
			xmlSB.append("<customerId>");
			xmlSB.append(biDZColor.getBiFranceCustomer().getId());
			xmlSB.append("</customerId>");
			
			xmlSB.append("<customerName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getBiFranceCustomer().getName());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerName>");
			
			xmlSB.append("<customerDepartment>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getBiFranceCustomer().getDepartment()==null?"":biDZColor.getBiFranceCustomer().getDepartment());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerDepartment>");
			
			xmlSB.append("<customerColorCode>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getCustomerColorCode() == null? "" : biDZColor.getCustomerColorCode());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerColorCode>");
		}
		
		if(biDZColor.getBiYarn().getBiFactory() != null){
			xmlSB.append("<factoryId>");
			xmlSB.append(biDZColor.getBiYarn().getBiFactory().getId());
			xmlSB.append("</factoryId>");
			
			xmlSB.append("<factoryNameCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getBiYarn().getBiFactory().getNameCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameCN>");
			
			xmlSB.append("<factoryNameEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getBiYarn().getBiFactory().getNameEN()==null?"":biDZColor.getBiYarn().getBiFactory().getNameEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameEN>");
			
			xmlSB.append("<addressFactoryCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getBiYarn().getBiFactory().getAddressFactoryCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryCN>");
			
			xmlSB.append("<addressFactoryEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getBiYarn().getBiFactory().getAddressFactoryEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryEN>");
			
			xmlSB.append("<supplierNumber>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biDZColor.getSupplierNumber() == null? "":biDZColor.getSupplierNumber());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</supplierNumber>");
		}
		
		xmlSB.append("<pantoneNumber>");
		xmlSB.append(biDZColor.getPantoneNumber() == null? "":biDZColor.getPantoneNumber());
		xmlSB.append("</pantoneNumber>");
		
		xmlSB.append("<realColor>");
		xmlSB.append(biDZColor.getRealColor()  == null? "":biDZColor.getRealColor());
		xmlSB.append("</realColor>");
		
		xmlSB.append("<yarnId>");
		xmlSB.append(biDZColor.getBiYarn().getId());
		xmlSB.append("</yarnId>");
		
		xmlSB.append("<yarnReference>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biDZColor.getBiYarn().getReference());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</yarnReference>");
		
		xmlSB.append("<ctDesignType>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtDesignType.class.getName(), oct.getCtDesignType(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctDesignType>");
		
		xmlSB.append("<ctSendBy>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtSendBy.class.getName(), oct.getCtSendBy(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctSendBy>");
		
		xmlSB.append("<ctYarnMeasure>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMeasure.class.getName(), biDZColor.getBiYarn().getCtYarnMeasure(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMeasure>");
		
		xmlSB.append("<ctYarnMeasure>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMeasure.class.getName(), biDZColor.getBiYarn().getCtYarnMeasure(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMeasure>");
		
		xmlSB.append("<yarnAccount>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biDZColor.getBiYarn().getYarnAccount());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</yarnAccount>");
		
		xmlSB.append("<ctYarnMaterial1>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial1(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial1()) && biDZColor.getBiYarn().getComposition1() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition1() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial1>");
		
		xmlSB.append("<ctYarnMaterial2>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial2(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial2()) && biDZColor.getBiYarn().getComposition2() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition2() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial2>");
		
		xmlSB.append("<ctYarnMaterial3>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial3(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial3()) && biDZColor.getBiYarn().getComposition3() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition3() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial3>");
		
		xmlSB.append("<ctYarnMaterial4>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial4(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial4()) && biDZColor.getBiYarn().getComposition4() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition4() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial4>");
		
		xmlSB.append("<ctYarnMaterial5>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial5(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial5()) && biDZColor.getBiYarn().getComposition5() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition5() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial5>");
		
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(oct));

		return xmlSB.toString();
	}
	
	public static void main(String[] args) throws Exception{
		OrderColorTestServiceImpl os = new OrderColorTestServiceImpl();
		System.out.println(os.getAll(2));
		//char index = 'C';
		//System.out.println(++index);
	}

}
